package DBConnection;

import java.util.List;

import model.entities.WeatherStationCollection;

import org.hibernate.HibernateException;

public class WeatherStationCollectionDB extends HibernateUtil implements
		ConnectDB<WeatherStationCollection, Boolean, Integer> {

	@Override
	public Boolean add(WeatherStationCollection WeatherStationCollection) {
		try {
			if (WeatherStationCollection != null) {
				this.beginTranSaction();
				this.getCurrentSession().save(WeatherStationCollection);
				this.commitTransaction();
			}
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@Override
	public Boolean delete(Integer ID) {
		WeatherStationCollection WeatherStationCollection = find(ID);
		try {
			if (WeatherStationCollection != null) {
				this.beginTranSaction();
				this.getCurrentSession().delete(WeatherStationCollection);
				this.commitTransaction();
			}
		} catch (HibernateException e) {
			this.rollback();
			throw e;
		}
		return true;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<WeatherStationCollection> findAll() {
		try {
			this.beginTranSaction();
			return this.getCurrentSession()
					.createCriteria(WeatherStationCollection.class).list();
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}

	@Override
	public Boolean update(WeatherStationCollection WeatherStationCollection) {
		try {
			if (WeatherStationCollection != null) {
				this.beginTranSaction();
				this.getCurrentSession().update(WeatherStationCollection);
				this.commitTransaction();
			}
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@Override
	public WeatherStationCollection find(Integer ID) {
		try {
			this.beginTranSaction();
			return (WeatherStationCollection) this.getCurrentSession().get(
					WeatherStationCollection.class, ID);
		} catch (Exception e) {
			throw e;
		}
	}
	public static void main(String[] args) {
		List<WeatherStationCollection> ll = new WeatherStationCollectionDB().findAll();
		System.out.println(ll.size());
		System.out.println(new WeatherStationCollectionDB().find(1));
	}

}
